package 比屋作业.day16;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class D16_1 {
    public static void main(String[] args) {
        Library library = new Library();

        // 添加图书
        library.addBooks(new Book("Java编程思想", "Bruce Eckel"));
        library.addBooks(new Book("Effective Java", "Joshua Bloch"));
        library.addBooks(new Book("Java核心技术", "Cay S. Horstmann"));

        // 显示所有图书
        library.displayBooks();

        // 查找图书
        System.out.println("\n查找《Effective Java》：");
        for (Book book : library.findBooks("Effective Java")) {
            System.out.println(book);
        }

        // 删除图书
        System.out.println("\n删除《Java核心技术》：");
        if (library.removeBook("Java核心技术", "Cay S. Horstmann")) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }

        // 再次显示所有图书
        System.out.println("\n更新后的图书列表：");
        library.displayBooks();
    }
}
class Book {
    private String title;
    private String author;

    public Book(String title, String author) {
        this.author = author;
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public String getTitle() {
        return title;
    }

    @Override
    public String toString() {
        return "《" + title + "》" + "-" + author;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Book book = (Book) obj;
        return this.author.equals(book.author) && this.title.equals(book.title);
    }

    @Override
    public int hashCode() {
        return title.hashCode() + author.hashCode();
    }
}
class Library{
    private Collection<Book> books;
    public Library(){
        this.books=new ArrayList<>();
    }
    public void addBooks(Book book){
        books.add(book);
        System.out.println("书籍添加成功！");
    }
    public boolean removeBook(String title,String author){
        Book target=new Book(title,author);
        return books.remove(target);
    }
    public List<Book> findBooks(String title){
        List<Book> result=new ArrayList<>();
        for (Book book:books){
            if (book.getTitle().equals(title)){
                result.add(book);
            }
        }
        return result;
    }
    public void displayBooks(){
        System.out.println("图书馆里的所有的书：");
        for (Book book:books){
            System.out.println(book);
        }
    }
}
